package phoenix.core.config.extensions;

import java.io.File;

import core.ApplicationManager;
import core.config.Config;
import core.helpers.PropertyLoaderHelper;

/**
 * Class extends the fields and methods of application configuration
 */
public class ConfigExt extends Config
{
	// Project properties file
	private final PropertyLoaderHelper projectProperties;
	// Mail properties file
	private static PropertyLoaderHelper mailProperties;
	// Redmine property file
	private static PropertyLoaderHelper redmineProperties;
	// Appium property file
	private static PropertyLoaderHelper appiumProperties;
	// Browser property file
	private static PropertyLoaderHelper browserProperty;

	// Backend access
	public String BACKEND_USERNAME;
	public String BACKEND_PASSWORD;
	public String BACKEND_URL_BASE;
	public String BACKEND_URL_LIVE;
	public String BACKEND_LOGIN_URL;
	public String BACKEND_SEARCH_URL;
	public String BACKEND_USER_EDIT;
	public String BACKEND_PAY_URL;
	public String BACKEND_NOTIFICATION_SUBSCRIBITION;
	public String BACKEND_NOTIFICATION_SETTINGS;

	// Redmine data
	public String REDMINE_HOST;
	public String REDMINE_API_ACCESS_KEY;
	public String REDMINE_PROJECT_KEY_WEB;
	public String REDMINE_PROJECT_KEY_WEB_REL;
	public String REDMINE_PROJECT_KEY_WEB_ADSBRIDGE;
	public String REDMINE_PROJECT_KEY_MOB;
	public Boolean REDMINE_POST_ERRORS;
	public String REDMINE_WEB_ISSUE_ID;
	public String REDMINE_WEB_ISSUE_ID_REL;
	public String REDMINE_WEB_ISSUE_ID_ADSBRIDGE;
	public String REDMINE_MOB_ISSUE_ID;
	public String REDMINE_WEB_ISSUE_WATCHER;
	public String REDMINE_MOB_ISSUE_WATCHER;

	// Settings for Redmine report
	public String REPORT_SUBJECT;
	public String REPORT_TO;
	public String REPORT_TO_CC;
	public String REPORT_FROM;
	public String REPORT_FILE;

	/**
	 * WEB *
	 */
	public String GOLOVKO;
	public String KOLOSKOV;
	public String BABAR;

	/**
	 * MOB *
	 */
	public String NAGIBIN;

	// Sub projects
	public final String SUBPROJECT_ADSBRIDGE = "adsbridge";

	// Locations IP
	public final static String LOCATION_DEF_IP = "176.58.88.82";
	public final static String LOCATION_GBR_IP = "176.58.88.82";
	public final static String LOCATION_USA_IP = "69.147.251.50";
	public final static String LOCATION_AUS_IP = "223.252.33.75";
	public final static String LOCATION_BRA_IP = "201.33.19.9";
	public final static String LOCATION_CAN_IP = "199.167.19.29";
	public final static String LOCATION_MEX_IP = "201.150.38.98";
	public final static String LOCATION_IND_IP = "111.118.186.245";
	public final static String LOCATION_NZL_IP = "49.50.242.137";
	public final static String LOCATION_NLD_IP = "178.237.42.37";
	public final static String LOCATION_SWE_IP = "91.189.44.162";
	public final static String LOCATION_NOR_IP = "81.27.33.8";
	public final static String LOCATION_ITA_IP = "195.88.7.112";
	public final static String LOCATION_ESP_IP = "91.142.213.109";
	public final static String LOCATION_FRA_IP = "5.135.39.76";
	public final static String LOCATION_DEU_IP = "80.237.249.248";
	public final static String LOCATION_ZAF_IP = "41.222.38.155";

	// Proxies
	public String PROXY_GBR = "p-uk1.biscience.com:80";
	public String PROXY_USA = "p-us1.biscience.com:80";
	public String PROXY_AUS = "p-au1.biscience.com:80";
	public String PROXY_BRA = "p-br1.biscience.com:80";
	public String PROXY_CAN = "p-ca2.biscience.com:80";
	public String PROXY_MEX = "p-mx1.biscience.com:80";
	public String PROXY_FIN = "p-fi1.biscience.com:80";
	public String PROXY_IND = "p-in1.biscience.com:80";
	public String PROXY_IRL = "p-ie1.biscience.com:80";
	public String PROXY_NZL = "p-nz1.biscience.com:80";
	public String PROXY_NLD = "p-nl1.biscience.com:80";
	public String PROXY_SWE = "p-se1.biscience.com:80";
	public String PROXY_NOR = "p-no1.biscience.com:80";
	public String PROXY_ITA = "p-it1.biscience.com:80";
	public String PROXY_ESP = "p-es1.biscience.com:80";
	public String PROXY_FRA = "p-fr1.biscience.com:80";

	// Locations
	public static final String LOCATION_ARG = "ARG";
	public static final String LOCATION_BRA = "BRA";
	public static final String LOCATION_GBR = "UK";
	public static final String LOCATION_USA = "US";
	public static final String LOCATION_AUS = "AUS";
	public static final String LOCATION_FRA = "FRA";
	public static final String LOCATION_AUT = "AUT";
	public static final String LOCATION_BEL = "BEL";
	public static final String LOCATION_CAN = "CAN";
	public static final String LOCATION_CHN = "CHN";
	public static final String LOCATION_CZE = "CZE";
	public static final String LOCATION_DNK = "DNK";
	public static final String LOCATION_DEU = "DEU";
	public static final String LOCATION_IND = "IND";
	public static final String LOCATION_INN = "INN";
	public static final String LOCATION_ITA = "ITA";
	public static final String LOCATION_JPN = "JPN";
	public static final String LOCATION_MYS = "MYS";
	public static final String LOCATION_MEX = "MEX";
	public static final String LOCATION_NLD = "NLD";
	public static final String LOCATION_NZL = "NZL";
	public static final String LOCATION_NOR = "NOR";
	public static final String LOCATION_PRT = "PRT";
	public static final String LOCATION_ZAF = "ZAF";
	public static final String LOCATION_ESP = "ESP";
	public static final String LOCATION_SWE = "SWE";
	public static final String LOCATION_CHE = "CHE";
	public static final String LOCATION_TUR = "TUR";
	public static final String LOCATION_FIN = "FIN";
	public static final String LOCATION_IRL = "IRL";
	public static final String LOCATION_RANDOM = "ANY";

	// Sites identifiers
	public final static String SITE_ID_CLICKANDFLIRT = "clickandflirt";
	public final static String SITE_ID_LOCALSGOWILD = "localsgowild";
	public final static String SITE_ID_MATUREAFFECTION = "matureaffection";
	public final static String SITE_ID_LOCALSGODATING = "localsgodating";
	public final static String SITE_ID_DATEFINDER = "datefinder";
	public final static String SITE_ID_EBONYFLIRT = "ebonyflirt";
	public final static String SITE_ID_CHEECKYLOVERS = "cheekylovers";
	public final static String SITE_ID_WILDBUDDIES = "wildbuddies";
	public final static String SITE_ID_BENAUGHTY = "benaughty";
	public final static String SITE_ID_FLIRT = "flirt";
	public final static String SITE_ID_SAUVAGESDUCOIN = "sauvagesducoin";
	public final static String SITE_ID_FOREIGNGIRLFRIEND = "foreigngirlfriend";
	public final static String SITE_ID_NAUGHTYFEVER = "naughtyfever";
	public final static String SITE_ID_MATUREDATING = "maturedating";
	public final static String SITE_ID_NAUGHTYDATE = "naughtydate";
	// Upload photo file path
	public final static String UPLOAD_PHOTO_MALE = System.getProperty("user.dir") + "\\src\\phoenix\\resources\\photos\\male\\male.jpg";
	public final static String UPLOAD_PHOTO_FEMALE = System.getProperty("user.dir") + "\\src\\phoenix\\resources\\photos\\female\\female.jpg";
	public final static String UPLOAD_PHOTO_NOT_APPROVED = System.getProperty("user.dir") + "\\src\\phoenix\\resources\\photos\\male\\gena.jpg";

	public static String WEBDRIVER_PATH;

	/**
	 * Initializes ConfigExt
	 * @param p_manager ApplicationManager p_app instance
	 */
	public ConfigExt(ApplicationManager p_manager)
	{
		super(p_manager);

		/*** PROJECT PROPERTIES ***/
		projectProperties = loadResFile("phoenix\\project.properties");
		// Http
		HTTP_USERNAME = getProjectProperty("http.username");
		HTTP_PASSWORD = getProjectProperty("http.password");

		// Backend
		BACKEND_USERNAME = getProjectProperty("backend.username");
		BACKEND_PASSWORD = getProjectProperty("backend.password");

		BACKEND_URL_BASE = (Config.BUILD.equals("live")) ? getProjectProperty("backend.url") : getProjectProperty("backend.rel.url");
		BACKEND_URL_LIVE = getProjectProperty("backend.url");

		// Proxy
		PROXY_AUTH = Boolean.parseBoolean(getProjectProperty("proxy.auth"));
		PROXY_USERNAME = getProjectProperty("proxy.username");
		PROXY_PASSWORD = getProjectProperty("proxy.password");

		// Data Base
		DB_HOST = getProjectProperty("db.host");
		DB_PORT = getProjectProperty("db.port");
		DB_NAME = getProjectProperty("db.name");
		DB_USER = getProjectProperty("db.user");
		DB_PASSWORD = getProjectProperty("db.password");

		WEBDRIVER_WAIT = Integer.parseInt(getProjectProperty("webdriver.wait"));
		WEBDRIVER_FLASH_ELEMENTS = Boolean.parseBoolean(getProjectProperty("webdriver.flash.element"));
		WEBDRIVER_CAPTURE_SCREENSHOT = Boolean.parseBoolean(getProjectProperty("webdriver.capture.screenshot"));
		WEBDRIVER_PATH = getProjectProperty("webdriver.path");
		// GoogleChrome driver
		File DRIVER_CHROME_PATH = new File(WEBDRIVER_PATH + "/chromedriver.exe");
		// InternetExplorer driver
		File DRIVER_IE_PATH = new File(WEBDRIVER_PATH + "/IEDriverServer.exe");
		System.setProperty("webdriver.ie.driver", DRIVER_IE_PATH.getAbsolutePath());
		System.setProperty("webdriver.chrome.driver", DRIVER_CHROME_PATH.getAbsolutePath());

		BACKEND_LOGIN_URL = BACKEND_URL_BASE + getProjectProperty("backend.login.url");
		BACKEND_SEARCH_URL = BACKEND_URL_BASE + getProjectProperty("backend.search.url");
		BACKEND_USER_EDIT = BACKEND_URL_BASE + getProjectProperty("backend.user.edit");
		BACKEND_PAY_URL = (Config.BUILD.equals(Config.BUILD_LIVE)) ?
				BACKEND_URL_BASE + "/admin" + getProjectProperty("backend.pay.url") :
				BACKEND_URL_BASE + getProjectProperty("backend.pay.url");
		BACKEND_NOTIFICATION_SUBSCRIBITION = BACKEND_URL_BASE + getProjectProperty("backend.notification.subscription");
		BACKEND_NOTIFICATION_SETTINGS = BACKEND_URL_BASE + getProjectProperty("backend.notification.settings");

		/*** MAIL PROPERTIES ***/
		mailProperties = loadResFile("phoenix\\mail.properties");

		MAIL_GATEWAY = getMailProperty("imap.gateway");
		MAIL_USER = getMailUser(MAIL_GATEWAY);
		MAIL_PASSWORD = getMailPassword(MAIL_GATEWAY);
		MAIL_HTML_PATH = getMailProperty("imap.html.path");
		MAIL_DELAY = getMailProperty("inbox.check.delay");

		SMTP_HOST = getMailProperty("smtp.host");
		SMTP_PORT = getMailProperty("smtp.port");
		SMTP_USER = getMailProperty("smtp.user");
		SMTP_PASSWORD = getMailProperty("smtp.password");

		/** REDMINE SETTINGS **/
		redmineProperties = loadResFile("phoenix\\redmine.properties");

		REDMINE_HOST = getRedmineProperty("redmine.host");
		REDMINE_API_ACCESS_KEY = getRedmineProperty("redmine.api.key");
		REDMINE_PROJECT_KEY_WEB = getRedmineProperty("redmine.project.key.web");
		REDMINE_PROJECT_KEY_WEB_REL = getRedmineProperty("redmine.project.key.web.rel");
		REDMINE_PROJECT_KEY_WEB_ADSBRIDGE = getRedmineProperty("redmine.project.key.adsbridge");
		REDMINE_PROJECT_KEY_MOB = getRedmineProperty("redmine.project.key.mob");
		REDMINE_POST_ERRORS = Boolean.parseBoolean(getRedmineProperty("redmine.post.errors"));
		REDMINE_WEB_ISSUE_ID = getRedmineProperty("redmine.parrent.id.web");
		REDMINE_WEB_ISSUE_ID_REL = getRedmineProperty("redmine.parrent.id.web.rel");
		REDMINE_MOB_ISSUE_ID = getRedmineProperty("redmine.parrent.id.mob");
		REDMINE_WEB_ISSUE_ID_ADSBRIDGE = getRedmineProperty("redmine.parrent.id.adsbridge");

		REPORT_SUBJECT = getRedmineProperty("redmine.report.subject");
		REPORT_TO = getRedmineProperty("redmine.report.to");
		REPORT_TO_CC = getRedmineProperty("redmine.report.to.cc");
		REPORT_FROM = getRedmineProperty("redmine.report.from");
		REPORT_FILE = getRedmineProperty("redmine.report.file");

		REDMINE_WEB_ISSUE_WATCHER = getRedmineProperty("redmine.group.web");
		REDMINE_MOB_ISSUE_WATCHER = getRedmineProperty("redmine.group.mob");

		GOLOVKO = getRedmineProperty("redmine.user.golovko");
		KOLOSKOV = getRedmineProperty("redmine.user.koloskov");
		BABAR = getRedmineProperty("redmine.user.babar");

		/**
		 * MOB *
		 */
		NAGIBIN = getRedmineProperty("redmine.user.nagibin");

		/** APPIUM SETTINGS **/
		appiumProperties = loadResFile("phoenix\\appium.properties");

		APPIUM_DEVICE = getPropertyValue(appiumProperties, "appium.device");
		if (!((APPIUM_DEVICE != "device") && (APPIUM_DEVICE != "emulator") && (APPIUM_DEVICE != "any")))
		{
			manager.log().warn(
					"Incorrect Value specified for 'Appium.Device' Property! Got - '" + APPIUM_DEVICE
							+ "', expected 'device' / 'emulator' or 'any'. Defaulting to 'ANY' behaviour");
			APPIUM_DEVICE = "any";
		}
		APPIUM_BROWSER = getPropertyValue(appiumProperties, "appium.browser");
		if (APPIUM_BROWSER.equals("default"))
		{
			APPIUM_BROWSER = "Browser";
		}

		APPIUM_PATH = getPropertyValue(appiumProperties, "appium.path");
		APPIUM_CHROMEDRIVER = getPropertyValue(appiumProperties, "appium.chromedriver");
		APPIUM_LOG = getPropertyValue(appiumProperties, "appium.log");
		APPIUM_LOGLEVEL = getPropertyValue(appiumProperties, "appium.log-level");
		APPIUM_COMMANDTIMEOUT = getPropertyValue(appiumProperties, "appium.command-timeout");
		APP_ACTIVITY_FLIRT = "com.datingflirty.ui.activity.AuthActivity";
		ANDROID_PACKAGE_FLIRT = "com.datingflirty";
		APP_ACTIVITY_WB = "com.dating.sdk.ui.activity.AuthActivity";
		ANDROID_PACKAGE_WB = "com.wbdating";
		APP_ACTIVITY_CL = "com.dating.sdk.ui.activity.AuthActivity";
		ANDROID_PACKAGE_CL = "com.cheeky";

		browserProperty = loadExtFile("config\\phoenix\\browser.properties");
		BROWSER_FIREFOX_PROFILE = getBrowserProperty("firefox.profile.name");
	}

	private String getBrowserProperty(String s)
	{
		if (browserProperty != null)
		{
			return browserProperty.load(s);
		}
		return null;
	}

	/**
	 * @param p_propertyName
	 * @return
	 */
	protected String getPropertyValue(PropertyLoaderHelper p_propertyContainer, String p_propertyName)
	{
		String propertyValue = null;

		if (p_propertyContainer != null)
		{
			try
			{
				propertyValue = p_propertyContainer.loadAsRes(p_propertyName);

				if (propertyValue == null)
				{
					throw new NullPointerException();
				}

				return propertyValue;
			}
			catch (NullPointerException p_ex)
			{
				manager.log().error("Property '" + p_propertyName + "' not found in '" + p_propertyContainer + "'");

				return null;
			}
		}
		else
		{
			manager.log().error("Property Container is NULL");
			return null;
		}

		// return propertyValue;
	}

	private String getMailUser(String p_gateway)
	{
		String result = null;
		switch (p_gateway)
		{
			case "yahoo":
				result = mailProperties.loadAsRes("imap.yahoo.user");
				break;
			case "hotmail":
				result = mailProperties.loadAsRes("imap.hotmail.user");
				break;
			default: // gmail
				result = mailProperties.loadAsRes("imap.gmail.user");
				break;
		}
		return result;
	}

	private String getMailPassword(String p_gateway)
	{
		String result = null;
		switch (p_gateway)
		{
			case "yahoo":
			{
				result = mailProperties.loadAsRes("imap.yahoo.password");
				break;
			}
			case "hotmail":
			{
				result = mailProperties.loadAsRes("imap.hotmail.password");
				break;
			}
			default: // gmail
			{
				result = mailProperties.loadAsRes("imap.gmail.password");
				break;
			}
		}

		return result;
	}

	/**
	 * @param p_propertyName
	 * @return
	 */
	protected String getProjectProperty(String p_propertyName)
	{
		try
		{
			String propertyValue = projectProperties.loadAsRes(p_propertyName);

			if (propertyValue == null)
			{
				throw new NullPointerException();
			}

			return propertyValue;
		}
		catch (NullPointerException p_ex)
		{
			manager.log().error("Project property not found: '" + p_propertyName + "'");

			return null;
		}
	}

	/**
	 * @param p_propertyName
	 * @return
	 */
	protected String getMailProperty(String p_propertyName)
	{
		return mailProperties.loadAsRes(p_propertyName);
	}

	/**
	 * @param p_propertyName
	 * @return
	 */
	protected String getRedmineProperty(String p_propertyName)
	{
		return redmineProperties.loadAsRes(p_propertyName);
	}

	public String getMailSettings()
	{
		return BACKEND_PASSWORD;

	}

	/**
	 * @param p_trackerName
	 * @return
	 */
	public int getRedmineTrackerID(String p_trackerName)
	{
		int trackerID;

		switch (p_trackerName)
		{
			case "bug":
			{
				trackerID = 1;
				break;
			}
			case "tech":
			{
				trackerID = 130;
				break;
			}
			default:
			{
				trackerID = 1;
			}
		}

		return trackerID;
	}

	/**
	 * @param p_fieldName
	 * @return
	 */
	public int getRedmineFieldID(String p_fieldName)
	{
		int fieldID;

		switch (p_fieldName)
		{
			case "zone":
			{
				fieldID = 82;
				break;
			}
			default:
			{
				fieldID = 0;
			}
		}

		return fieldID;
	}

	/**
	 * @param p_priorityName
	 * @return
	 */
	public int getRedminePriorityID(String p_priorityName)
	{
		int priorityID;

		switch (p_priorityName)
		{
			case "low":
			{
				priorityID = 7;
				break;
			}
			case "normal":
			{
				priorityID = 10;
				break;
			}
			case "high":
			{
				priorityID = 13;
				break;
			}
			case "urgent":
			{
				priorityID = 16;

				break;
			}
			case "immediate":
			{
				priorityID = 19;
				break;
			}
			default:
			{
				priorityID = 10;
			}
		}

		return priorityID;
	}

	/**
	 * Gets issue status 'id' by 'name'
	 * @return Issue status ID
	 */
	public int getRedmineStatusID(String p_statusName)
	{
		int statusID;

		switch (p_statusName)
		{
			case "new":
			{
				statusID = 1;
				break;
			}
			case "in progress":
			{
				statusID = 4;
				break;
			}
			case "closed":
			{
				statusID = 13;
				break;
			}
			case "canceled":
			{
				statusID = 16;
				break;
			}
			default:
			{
				statusID = 1;
			}
		}

		return statusID;
	}

	/**
	 * Setter for redmine issue ID mob
	 * @param p_statusName
	 */
	public void setRedmineMobIssueId(String p_statusName)
	{
		REDMINE_MOB_ISSUE_ID = p_statusName;
	}

	/**
	 * Getter for redmine issue ID Web
	 * @return String redmine ID of Web
	 */
	public String getRedmineWebIssueId()
	{
		return REDMINE_WEB_ISSUE_ID;
	}

	/**
	 * Getter for redmine issue ID Mob
	 * @return String redmine ID of Web
	 */
	public String getRedmineMobIssueId()
	{
		return REDMINE_MOB_ISSUE_ID;
	}
}
